package com.yh.action;

import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;
import com.yh.model.UserModel;
import com.yh.service.UserService;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.util.ServletContextAware;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

public class LoginAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, ServletContextAware {

    private String uname;
    private String upwd;
    private String phone;
    private String rename;
    private String nextDispose;

    public String getUname() {
        return uname;
    }

    public void setUname(String uname) {
        this.uname = uname;
    }

    public String getUpwd() {
        return upwd;
    }

    public void setUpwd(String upwd) {
        this.upwd = upwd;
    }

    public String getPhone() {
        return phone;
    }

    public void setPhone(String phone) {
        this.phone = phone;
    }

    public String getRename() {
        return rename;
    }

    public void setRename(String rename) {
        this.rename = rename;
    }

    public String getNextDispose() {
        return nextDispose;
    }

    public void setNextDispose(String nextDispose) {
        this.nextDispose = nextDispose;
    }

    private HttpServletRequest request;
    private HttpServletResponse response;
    private ServletContext context;

    /**
     * 用户登录
     * @return
     * @throws Exception
     */
    public String login() throws Exception{
        String result = "";
        try {
            List<UserModel> users =  new UserService().login(phone,upwd);
            if (users.size() == 1){
                this.request.getSession().setAttribute("loginuser",this.phone);
                result = Action.SUCCESS;
                String isno = new UserService().isno(phone,upwd);
                if (isno.equals("y")){
                    nextDispose = "page/guanli.htm";
                    request.getSession().setAttribute("userid",users.get(0).getUid());
                }
                if (isno.equals("n")){
                    nextDispose = "page/list.htm";
                }
            }
            if (users.size() == 0){
                result = Action.ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    /**
     * 用户退出
     * @return
     * @throws Exception
     */
    public String quit() throws Exception{
        this.request.getSession().removeAttribute("loginuser");
        this.request.getSession().removeAttribute("userid");
        String result = "";
        result = Action.NONE;
        return result;
    }

    /**
     * 用户注册
     * @return
     * @throws Exception
     */
    public String regist() throws Exception{
        String result = "";
        try {
            int a = new UserService().regist(uname,upwd,phone,rename);
            if (a == 1){
                this.request.getSession().setAttribute("loginuser",this.uname);
                result = Action.SUCCESS;
                nextDispose = "page/list.htm";
            }
            if (a == 0){
                result = Action.ERROR;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    @Override
    public void setServletRequest(HttpServletRequest httpServletRequest) {
        this.request = httpServletRequest;
    }

    @Override
    public void setServletResponse(HttpServletResponse httpServletResponse) {
        this.response = httpServletResponse;
    }

    @Override
    public void setServletContext(ServletContext servletContext) {
        this.context = servletContext;
    }
}
